package asia.doing.service.manage;
import asia.doing.entity.*;
import asia.doing.mapper.ArticleMapper;
import asia.doing.mapper.CategoryMapper;
import asia.doing.mapper.TagMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

@Service(value = "manageArticleService")
public class ArticleService {
    @Resource
    private ArticleMapper articleMapper;

    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private TagMapper tagMapper;

    public List<Article> selectArticle(Article article) {
        List<Article> articles = articleMapper.selectAll(article);
        for (Article articleItem : articles) {
            int articleId = articleItem.getId();
            ArticleCategory articleCategory;
            articleCategory = new ArticleCategory();
            articleCategory.setArticleId(articleId);
            List<ArticleCategory> articleCategories = categoryMapper.selectByArtOrCaId(articleCategory);
            HashSet<Integer> categoryIds = new HashSet<>();
            for (ArticleCategory articleCategoryItewm : articleCategories) {
                if(articleId == articleCategoryItewm.getArticleId()){
                    categoryIds.add(articleCategoryItewm.getCategoryId());
                }
            }
            //拿到菜单ids
            Integer[] categoryIdsArray = categoryIds.toArray(new Integer[categoryIds.size()]);
            //开始查询一个个菜单
            List<Category> categories = new ArrayList<>();
            for (Integer i : categoryIdsArray) {
                List<Category> categories1 = categoryMapper.selectAll(new Category(i));
                categories.add(categories1.get(0));
            }



            ArticleTag articleTag;
            articleTag = new ArticleTag();
            articleTag.setArticleId(articleId);
            List<ArticleTag> articleTags = tagMapper.selectByArtOrTagId(articleTag);
            HashSet<Integer> tagIds = new HashSet<>();
            for (ArticleTag articleTagItewm : articleTags) {
                if(articleId == articleTagItewm.getArticleId()){
                    tagIds.add(articleTagItewm.getTagId());
                }
            }
            Integer[] tagIdsArray = tagIds.toArray(new Integer[tagIds.size()]);
            //开始查询一个个菜单
            List<Tag> tags = new ArrayList<>();
            for (Integer i : tagIdsArray) {
                List<Tag> tags1 = tagMapper.selectAll(new Tag(i));
                tags.add(tags1.get(0));
            }


            //将分类和标签添加到article类中
            articleItem.setCategoryList(categories);
            articleItem.setTagList(tags);
        }
        return articles;
    }

    public void addArticle(Article article) {
        LocalDateTime localDateTime = LocalDateTime.now();
        article.setCreateTime(localDateTime);
        article.setUpdateTime(localDateTime);
        Integer row = articleMapper.insert(article);
        Integer articleId = article.getId();
        System.out.println("影响的行数是:" + row);
        System.out.println("新增id为:" + article.getId());
        Integer[] categoryIds = article.getCategoryIds();
        Integer[] tagIds = article.getTagIds();
        if (categoryIds != null && categoryIds.length > 0) {
            for (Integer categoryId : categoryIds) {
                //添加新数据
                categoryMapper.insertByArtId(articleId, categoryId);
            }
        }
        if (tagIds != null && tagIds.length > 0) {
            for (Integer tagId : tagIds) {
                tagMapper.insertByArtId(articleId, tagId);
            }
        }
    }

    public void updateArticle(Article article) {
        LocalDateTime localDateTime = LocalDateTime.now();
        article.setUpdateTime(localDateTime);
        Integer row = articleMapper.updateById(article);
        Integer articleId = article.getId();
        System.out.println("影响的行数是:" + row);
        Integer[] categoryIds = article.getCategoryIds();
        Integer[] tagIds = article.getTagIds();

        // 删除旧的分类关联
        categoryMapper.deleteByArtId(articleId);
        // 插入新的分类关联
        if (categoryIds != null && categoryIds.length > 0) {
            for (Integer categoryId : categoryIds) {
                categoryMapper.insertByArtId(articleId, categoryId);
            }
        }

        // 删除旧的标签关联
        tagMapper.deleteByArtId(articleId);
        // 插入新的标签关联
        if (tagIds != null && tagIds.length > 0) {
            for (Integer tagId : tagIds) {
                tagMapper.insertByArtId(articleId, tagId);
            }
        }

    }

    public void deleteArticle(Integer id) {
        articleMapper.deleteById(id);
        // 删除旧的分类关联
        categoryMapper.deleteByArtId(id);
        // 删除旧的标签关联
        tagMapper.deleteByArtId(id);
    }

    public PageInfo<Article> selectPage(Article article, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        //注意:这里获取到的信息是重复的
        List<Article> list = articleMapper.selectAll(article);
        PageInfo<Article> pageInfo = PageInfo.of(list);
        List<Article> articles = pageInfo.getList(); //可以获取所有列表循环做其他事情,这个传递地址值
        //查询之后进行遍历，每个Article对象中有两个数组，一个是categoryIds，一个是tagIds
        for (Article articleItem : articles) {
            int articleId = articleItem.getId();
            ArticleCategory articleCategory;
            articleCategory = new ArticleCategory();
            articleCategory.setArticleId(articleId);
            List<ArticleCategory> articleCategories = categoryMapper.selectByArtOrCaId(articleCategory);
            HashSet<Integer> categoryIds = new HashSet<>();
            for (ArticleCategory articleCategoryItewm : articleCategories) {
                if(articleId == articleCategoryItewm.getArticleId()){
                    categoryIds.add(articleCategoryItewm.getCategoryId());
                }
            }
            //拿到菜单Id
            Integer[] categoryIdsArray = categoryIds.toArray(new Integer[categoryIds.size()]);
            //开始查询一个个菜单
            List<Category> categories = new ArrayList<>();
            for (Integer i : categoryIdsArray) {
                List<Category> categories1 = categoryMapper.selectAll(new Category(i));
                categories.add(categories1.get(0));
            }


            ArticleTag articleTag;
            articleTag = new ArticleTag();
            articleTag.setArticleId(articleId);
            List<ArticleTag> articleTags = tagMapper.selectByArtOrTagId(articleTag);
            HashSet<Integer> tagIds = new HashSet<>();
            for (ArticleTag articleTagItewm : articleTags) {
                if(articleId == articleTagItewm.getArticleId()){
                    tagIds.add(articleTagItewm.getTagId());
                }
            }
            //拿到标签Id
            Integer[] tagIdsArray = tagIds.toArray(new Integer[tagIds.size()]);
            //开始查询一个个菜单
            List<Tag> tags = new ArrayList<>();
            for (Integer i : tagIdsArray) {
                List<Tag> tags1 = tagMapper.selectAll(new Tag(i));
                tags.add(tags1.get(0));
            }


            //将分类和标签添加到article类中
            articleItem.setCategoryList(categories);
            articleItem.setTagList(tags);
        }
        return pageInfo;
    }

}
